/*
test_filesystem.cpp
*/

#include <stdlib.h>
#include <stdio.h>
#include "fs/FileSystem.h"
#include <sstream>

int main(int argc, char **argv){

    FileSystem * file_system;
    int r;

    //create the file_system
    file_system = new FileSystem();
    r = file_system->Mkfs();

    //make sure it was set up correctly
    if(r != 0){
        printf("Error Mkfs failed\n");
        delete file_system;
        return -1;
    }

    /****************
    TEST 1: PASSED
    ****************/
    /*
    stringstream ss;
    int i;
    for(i=0;i<100;i++){
        ss.str("");
        ss << "d" << i;
        file_system->MkDir(ss.str());
    }

    cout << endl << "----------------Start Tree()--------------" << endl;
    file_system->Tree();
    cout << "----------------End Tree()--------------" << endl << endl;
    
    */

    /****************
    TEST 2: PASSED
    ****************/
    /*
    r = file_system->Import("rand_70MB","sim_rand_70MB");
    if(r == -1){
        printf("If the file rand_70MB is not found, run the command 'dd if=/dev/urandom of=rand_70MB bs=1M count=70'\n");
    }
    r = file_system->Export("sim_rand_70MB","sim_rand_70MB");

    r = file_system->Import("rand_1MB","sim_rand_1MB");
    if(r == -1){
        printf("If the file rand_1MB is not found, run the command 'dd if=/dev/urandom of=rand_1MB bs=1M count=1'\n");
    }
    r = file_system->Export("sim_rand_1MB","sim_rand_1MB");
    */

    /****************
    Test 3:PASSED
    ****************/
    /*
    file_system->MkDir("a0");
    file_system->MkDir("a0/a1");
    file_system->MkDir("a0/a1/a2");
    file_system->MkDir("./b0/");
    file_system->MkDir("/c0");
    file_system->MkDir("/"); //invalid
    file_system->MkDir("d0/");
    file_system->MkDir("/e0//");
    file_system->MkDir("a0/a1/a2"); //duplicate
    file_system->MkDir("e0/../a0/a1/a22");
    file_system->Stat("./a0/a1");

    cout << endl << "----------------Start Tree()--------------" << endl;
    file_system->Tree();
    cout << "----------------End Tree()--------------" << endl << endl;
    */

    /****************
    TEST 4:
    ****************/

    /*
    file_system->MkDir("a0");
    file_system->MkDir("a0/a1");
    file_system->MkDir("a0/a2");

    cout << endl << "----------------Start Tree(added a0,a1,a2)--------------" << endl;
    file_system->Tree();
    cout << "----------------End Tree()--------------" << endl << endl;
    file_system->Stat("/a0");
    cout << endl;

    file_system->RmDir("/a0/a2");
    cout << endl << "----------------Start Tree(removed a0/a2)--------------" << endl;
    file_system->Tree();
    cout << "----------------End Tree()--------------" << endl << endl;
    file_system->Stat("/a0");
    
    file_system->MkDir("a0/a2");
    cout << endl << "----------------Start Tree (added a0/a2)--------------" << endl;
    file_system->Tree();
    cout << "----------------End Tree()--------------" << endl << endl;
    file_system->Stat("/a0");

    return 0;
    file_system->Cd("/");
    file_system->MkDir("./b0/");
    file_system->MkDir("/c0");
    file_system->Stat("/a0/a1");
    file_system->Stat("./a0/a3");
    file_system->MkDir("/"); //invalid
    file_system->MkDir("d0/");
    file_system->MkDir("/e0//");
    file_system->MkDir("a0/a1/a2"); //duplicate
    file_system->MkDir("e0/../a0/a1/a22");
    file_system->Cd("..");
    file_system->Ls();
    cout << "Rmdir about to be called" << endl;
    file_system->RmDir("foo");
    file_system->RmDir("e0");
    file_system->RmDir("a0");
    cout << "rmdir called" << endl;
    file_system->Stat("./a0/a1");
    file_system->Stat("/");

    cout << endl << "----------------Start Tree()--------------" << endl;
    file_system->Tree();
    cout << "----------------End Tree()--------------" << endl << endl;
    
         
    unsigned int thefp;
    char* buffer1 = "testtestabcdefghijklmnopqrstuvwxyz";
    char* buffer2 = (char*) malloc(100);
    char* buffer3 = (char*) malloc(100);
    char* buffer4 = (char*) malloc(100);
    char* buffer5 = (char*) malloc(100);
    
    //file_system->MkDir("a0");
    
    thefp = file_system->Open("test.txt", "rw");
    file_system->Write(thefp, buffer1);
    //file_system->Seek(thefp, 0);
    //file_system->Read(thefp, 100, buffer2);
    cout << "Buffer after read/write 1: " << buffer2 << endl;
    file_system->Close(thefp);
    
    
    buffer1 = "blahblah";
    thefp = file_system->Open("test.txt", "rw");
    file_system->Seek(thefp, 0);
    file_system->Write(thefp, buffer1);
    file_system->Seek(thefp, 0);
    file_system->Read(thefp, 100, buffer2);
    cout << "Buffer after read/write 2: " << buffer2 << endl;
    file_system->Close(thefp);
    
    thefp = file_system->Open("test.txt", "rw");
    file_system->Seek(thefp, 0);
    file_system->Read(thefp, 100, buffer2);
    cout << "Buffer after read 3: " << buffer2 << endl;
    file_system->Close(thefp);
    
    file_system->Cp("test.txt", "test2.txt");
    
    thefp = file_system->Open("test2.txt", "rw");
    file_system->Seek(thefp, 0);
    file_system->Read(thefp, 100, buffer4);
    cout << "Buffer after Cp: " << buffer4 << endl;
    
    file_system->Ls();
    
    file_system->Cd("/a0");
    
    file_system->Ls();
    
    delete file_system;

     * 
     */
    /*
     * print out exiting message
     */


    /****************
    Test 5:
    Import a MB file.
    Read its contents.
    Write its contents.
    Export the file.
    It should be 10MB, and equivalent to the original file x2.
    ****************/
    unsigned int thefp;
    /*
    string str;
    file_system->Import("test_files/rand_1MB","rand_1MB");
    thefp = file_system->Open("rand_1MB", "r");
    while(file_system->Read(thefp,27) > 0);
    thefp = file_system->Open("rand_1MB", "rw");
    file_system->Close(thefp);
    cout << "changing to " << save_mid_dir << endl;
    r = file_system->Cd(save_mid_dir);
    file_system->Import("test_files/rand_5MB","rand_5MB");
    thefp = file_system->Open("rand_5MB", "r");
    file_system->Close(thefp);
    */

    file_system->Cd("/");

    int i,j,k;
    stringstream ss,ss2;
    string save_mid_dir;
    string str;
    str = "012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678\n"; //100 bytes
    file_system->Import("test_files/1_mil_alphabet_lines","1");
    file_system->Cp("1","2");
    file_system->Export("1","file1_out.txt");
    file_system->Export("2","file2_out.txt");
    /*
    thefp = file_system->Open("0", "rw");
    cout << "opened fd = " << thefp << endl;
    file_system->Cat("0");
    for(i=0;i<2;i++){
        ss.str("");
        ss << "a" << i << "/";
        file_system->MkDir(ss.str());
        file_system->Cd(ss.str());
        for(j=0;j<3;j++){
            ss2.str("");
            ss2 << "b" << j; 
            file_system->MkDir(ss2.str());
            file_system->Cd(ss2.str());
            for(k=0;k<1000;k++){
                file_system->Write(thefp,str);
            }
            file_system->Close(thefp);
            file_system->Cd("..");
        }
        file_system->Cd("/");
    }
    */

    printf("Exiting...\n");
    delete file_system;
    return 0;

}
